package services

import (
	"regexp"
	"strings"
)

type MineRule struct {
}

// 全局变量

// 前19个字符,日期时间
func (r *MineRule) GetActionTime(str string) string {
	return str[:19]
}

func (r *MineRule) HandleFields(str string) (string, string, string, string, string, string, string, string, string) {
	typeTxt := ""
	trap_name := ""
	location := ""
	regType := regexp.MustCompile(`\[LogTrap]\s+(.*?)\.`)
	strsliceType := regType.FindStringSubmatch(str)
	if len(strsliceType) == 0 {
		return "", "", "", "", "", "", "", "", ""
	}
	typeTxt = strsliceType[1]
	//#region cond1判断Triggered.存在
	cond := false
	if strings.Contains(str, "Triggered.") {
		cond = true
	}
	userId := ""
	userName := ""
	streamId := ""
	ownerId := ""
	ownerName := ""
	ownerStreamId := ""
	//以User:开头，中间任意字符，以.结尾的字符串
	regAll := regexp.MustCompile(`User:\s+(.+?)\((\d+),\s+(\d+)\)\.\s+Trap name:\s*([^\.]+)\..*Location:(.*)`)
	strsliceAll := regAll.FindStringSubmatch(str)
	if len(strsliceAll) == 0 {
		return "", "", "", "", "", "", "", "", ""
	}
	userId = strsliceAll[2]
	userName = strsliceAll[1]
	streamId = strsliceAll[3]
	trap_name = strsliceAll[4]
	location = strsliceAll[5]
	if cond {
		regOwner := regexp.MustCompile(`Owner:\s+(.+?)\((\d+),\s+(\d+)\)\.`)
		strsliceOwner := regOwner.FindStringSubmatch(str)
		if len(strsliceOwner) > 0 {
			ownerId = strsliceOwner[2]
			ownerName = strsliceOwner[1]
			ownerStreamId = strsliceOwner[3]
		}
	}
	return typeTxt, trap_name, userId, userName, streamId, ownerId, ownerName, ownerStreamId, location
}
